<template>
	<view class="container">
		<van-sticky v-if="role != 'driver'">
			<van-dropdown-menu :active-color="primaryColor">
				<van-dropdown-item :value="status" :options="statusList" @change="onSelectStatus" />
				<van-dropdown-item ref="datePicker" :title="date">
					<view>
						<van-datetime-picker
						  type="date"
						  @input="onSelectDate"
						  :value="currentTime"
						  :show-toolbar="false"
						/>
						<view style="margin-top: 20px;display: flex;flex-flow: row nowrap;">
							<view style="flex: 2;">
								<van-button :color="primaryColor" :square="true" size="large" @click="onSelectTime">确认</van-button>
							</view>
							<view style="flex: 1;">
								<van-button :color="secondaryColor" :square="true" size="large" @click="onSelectAllTime">全部时间</van-button>
							</view>
						</view>
					</view>
				</van-dropdown-item>
			</van-dropdown-menu>
		</van-sticky>
		
		
		<view style="margin-top: 20px;"></view>
		<m-apply-card @click="onSelectCard" :item="item" :key="index" v-for="(item, index) in dataList"/>
	</view>
</template>

<script>
	import utils from '@/common/utils.js'
	export default {
		data() {
			return {
				primaryColor: getApp().globalData.primaryColor,
				secondaryColor: getApp().globalData.secondaryColor,
				status: 'all',
				role:'',
				// selectTime: new Date().getTime(),
				selectTime:'',
				selectTimeTemp:0,
				statusList:  [{
						text: '全部状态',
						value: 'all'
					}],
				dataList: [],
				dateStart: '1971/01/01 00:00',
				dateEnd: '2099/01/01 00:00',
				pageObj:{
					page:1,
					pagesize:10
				}
			};
		},
		async onLoad(options) {
			this.role = options.role
			if(this.role == 'user'){
				uni.setNavigationBarTitle({
					title:"我的申请"
				})
			}else if(this.role == 'reviewer'){
				uni.setNavigationBarTitle({
					title:"用车审核"
				})
			}else if(this.role == 'controller'){
				uni.setNavigationBarTitle({
					title:"用车调度"
				})
			}else if(this.role == 'driver'){
				uni.setNavigationBarTitle({
					title:"我的行程"
				})
			}
			
		},
		async onReachBottom() {
			this.pageObj.page++
			await this.getData()
		},
		async onPullDownRefresh(){
			await this.reloadData()
		},
		async onShow(){
			if(this.role == 'reviewer'){
				this.status = 'created'
			}else if(this.role == 'controller'){
				this.status = 'controllerAll'
			}
			await this.reloadData()
		},
		mounted() {
			if(this.role == 'user'){
				this.statusList = [{
						text: '全部状态',
						value: 'all'
					},{
						text: '待审核',
						value: 'created'
					},
					{
						text: '待调度',
						value: 'allowed'
					},
					{
						text: '待出发',
						value: 'dispatched'
					},
					{
						text: '已出发',
						value: 'travelStarted'
					},
					{
						text: '已到达',
						value: 'travelEnded'
					},
					{
						text: '待确认',
						value: 'adminComfirmed'
					},
					{
						text: '已完成',
						value: 'finished'
					},
					{
						text: '已取消',
						value: 'cancled'
					},
					{
						text: '未通过',
						value: 'refused'
					},
					{
						text: '已关闭',
						value: 'closed'
					}
				]
			}else if(this.role == 'reviewer'){
				// this.statusList =  [{
				// 		text: '全部状态',
				// 		value: 'reviewerAll'
				// 	}],
				this.statusList = [{
						text: '待审核',
						value: 'created'
					},
					{
						text: '已同意',
						value: 'allowed'
					},
					{
						text: '已拒绝',
						value: 'refused'
					}
				]
			}else if(this.role == 'controller'){
				// this.statusList =  [{
				// 		text: '全部状态',
				// 		value: 'controllerAll'
				// 	}],
				this.statusList = [{
						text: '全部状态',
						value: 'controllerAll'
					},{
						text: '待调度',
						value: 'allowed'
					},
					{
						text: '待出发',
						value: 'dispatched'
					},
					// {
					// 	text: '已转发',
					// 	value: 'forward'
					// },
					{
						text: '已取消',
						value: 'cancled'
					},
					{
						text: '已完成',
						value: 'finished'
					}
				]
			}
		},
		methods: {
			onSelectDate(event){
				this.selectTimeTemp = event.detail
			},
			async onSelectTime(){
				this.selectTime = this.selectTimeTemp
				this.$refs.datePicker.toggle()
				this.dateStart = `${this.date} 00:00`
				this.dateEnd = `${this.date} 23:59`
				await this.reloadData()
			},
			async onSelectStatus(event){
				this.status = event.detail
				await this.reloadData()
			},
			async reloadData(){
				this.pageObj.page = 1
				this.dataList = []
				await this.getData()
			},
			onSelectCard(obj){
				// console.log(obj)
				// return
				console.log(obj)
				const enData = encodeURIComponent(JSON.stringify(obj))
				console.log(enData)
				if(this.role == 'driver'){
					uni.navigateTo({
						//todo id
						url:`../route-detail/route-detail?role=${this.role}&data=${enData}`
					})
				}else{
					uni.navigateTo({
						//todo id
						url:`../bus-detail/bus-detail?role=${this.role}&data=${enData}`
					})
				}
				
			},
			async getData(){
				try{
					const userinfo = uni.getStorageSync('userinfo')
					if(this.status == 'forward'){
						this.status = 'dispatched'
					}
					const reqData={
						...this.pageObj,
						statuses: [this.status],
						token:userinfo.token,
						dateStart: this.dateStart,
						dateEnd: this.dateEnd
					}
					
					
					let url = ''
					if(this.role == 'user'){
						url = '/order/queryUserOrders'
					}
					else if(this.role == 'reviewer'){
						url = '/order/queryReviewerOrders'
					}else if(this.role == 'driver'){
						url = '/order/getDriverOrders'
					}else if(this.role == 'controller'){
						if(this.isThird){
							reqData.isThird = true
						}
						url = '/order/queryControllerOrders'
					}
					let rspData = await utils.req(url, reqData)
					if(this.status == 'forward'){
						rspData = rspData.filter(o => o.thirdCompanyId)
					}
					rspData.list = rspData.list.map(o => {
						o.passengers = JSON.parse(o.passengers)
						return o
					})
					this.dataList = [...this.dataList,...rspData.list]
				}catch(e){
					console.log(e)
				}
			},
			async onSelectAllTime(){
				this.selectTime = null 
				this.dateStart = '1971-01-01 00:00';
				this.dateEnd = '2099-01-01 00:00';
				this.$refs.datePicker.toggle()
				await this.getData()
			}
		},
		computed:{
			date(){
				if(!this.selectTime) return '全部时间'
				return utils.timeToStr(this.selectTime, "YYYY-MM-DD")
			},
			currentTime(){
				if(this.date == "全部时间") return new Date().getTime()
			},
			isThird(){
				return uni.getStorageSync('userinfo').companyId != 1
			}
		}
	}
</script>

<style lang="scss">

</style>
